package com.xxxy.df.baseui;

import android.graphics.drawable.Drawable;
import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class StudentRegisteActivity extends AppCompatActivity {
    EditText etName,etLOginname,etPwd,etPhone;
    RadioButton rbMale,rbFemale;
    CheckBox cbRead,cbSing,cbRun,cbSwim;
    ImageView ivProfile;
    Button btnReg;
    TextView tvResult;
    ConstraintLayout cl;
    Boolean flag = true; // true用toux2，false用toux1

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_student_registe);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        init();
    }
    private void init(){
        etName = findViewById(R.id.et_name);
        etLOginname = findViewById(R.id.et_loginname);
        etPwd = findViewById(R.id.et_password);
        etPhone = findViewById(R.id.et_phone);
        rbMale = findViewById(R.id.rb_male);
        rbFemale = findViewById(R.id.rb_female);
        cbRead = findViewById(R.id.cb_reading);
        cbSing = findViewById(R.id.cb_sing);
        cbRun = findViewById(R.id.cb_run);
        cbSwim = findViewById(R.id.cb_swim);
        ivProfile = findViewById(R.id.iv_profile);
        btnReg = findViewById(R.id.btn_reg);
        tvResult = findViewById(R.id.tv_result);
        cl = findViewById(R.id.main);
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etName==null||etName.getText().toString().trim().equals("")){
                    Toast.makeText(StudentRegisteActivity.this,"姓名不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(etLOginname==null||etLOginname.getText().toString().trim().equals("")){
                    Toast.makeText(StudentRegisteActivity.this,"用户名不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(etPwd==null||etPwd.getText().toString().trim().equals("")){
                    Toast.makeText(StudentRegisteActivity.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
                    return;
                }
                StringBuffer result = new StringBuffer();
                result.append("姓名："+etName.getText().toString().trim());//取到输入的文本转成字符串并去掉前后空格
                result.append("\n电话："+etPhone.getText().toString().trim());
                result.append("\n用户名："+etLOginname.getText().toString().trim());
                result.append("\n密码："+etPwd.getText().toString().trim());
                if(rbMale.isChecked()){
                    result.append("\n性别："+rbMale.getText().toString().trim());
                }else{
                    result.append("\n性别："+rbFemale.getText().toString().trim());
                }
                String hobby="";
//                if(cbRead.isChecked()) hobby += cbRead.getText().toString().trim()+"、";
//                if(cbSing.isChecked()) hobby += cbSing.getText().toString().trim()+"、";
//                if(cbRun.isChecked()) hobby += cbRun.getText().toString().trim()+"、";
//                if(cbSwim.isChecked()) hobby += cbSwim.getText().toString().trim();
                //循环获取CheckBox的状态
                for(int i=0;i<cl.getChildCount();i++){
                    View view = cl.getChildAt(i);
                    if(view instanceof CheckBox) {
                        if(((CheckBox) view).isChecked()){
                            hobby += ((CheckBox) view).getText().toString().trim()+"、";
                        }
                    }
                }
                //去除后面的顿号
                if(!hobby.equals("")&&hobby.lastIndexOf("、")>=hobby.length()-1){
                    hobby = hobby.substring(0,hobby.lastIndexOf("、"));
                }
                result.append("\n爱好："+hobby);
                //点击按钮换头像
                //方法1：
                Drawable image = ContextCompat.getDrawable(StudentRegisteActivity.this,R.drawable.toux2);
                ivProfile.setImageDrawable(image);
                //方法2：
                //ivProfile.setImageResource(R.drawable.toux2);
                tvResult.setText(result.toString());
            }
        });
        ivProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(flag == true){
                    ivProfile.setImageResource(R.drawable.toux2);
                    ivProfile.setAlpha(0f);
                    ivProfile.animate().alpha(1f).setDuration(1000).setListener(null).start();
                    flag = false;
                } else {
                    ivProfile.setImageResource(R.drawable.toux1);
                    flag = true;
                }
            }
        });

    }
}